<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>MiXiM: D:/Projects/MiXiM/extsrc/mixim/examples/CSMAMac/SimpleNetwLayer.h Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">MiXiM
   &#160;<span id="projectnumber">2.3</span>
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li><a href="dirs.html"><span>Directories</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('a00239.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">SimpleNetwLayer.h</div>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * SimpleNetwLayer.h</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> *  Created on: 29.08.2008</span>
<a name="l00005"></a>00005 <span class="comment"> *      Author: Karl Wessel</span>
<a name="l00006"></a>00006 <span class="comment"> */</span>
<a name="l00007"></a>00007 
<a name="l00008"></a>00008 <span class="preprocessor">#ifndef SIMPLENETWLAYER_H_</span>
<a name="l00009"></a>00009 <span class="preprocessor"></span><span class="preprocessor">#define SIMPLENETWLAYER_H_</span>
<a name="l00010"></a>00010 <span class="preprocessor"></span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &lt;cassert&gt;</span>
<a name="l00012"></a>00012 <span class="preprocessor">#include &lt;omnetpp.h&gt;</span>
<a name="l00013"></a>00013 <span class="preprocessor">#include &lt;sstream&gt;</span>
<a name="l00014"></a>00014 
<a name="l00015"></a>00015 <span class="preprocessor">#include &quot;BaseModule.h&quot;</span>
<a name="l00016"></a>00016 <span class="preprocessor">#include &quot;NetwPkt_m.h&quot;</span>
<a name="l00017"></a>00017 <span class="preprocessor">#include &quot;SimpleAddress.h&quot;</span>
<a name="l00018"></a>00018 <span class="preprocessor">#include &quot;MacToNetwControlInfo.h&quot;</span>
<a name="l00019"></a>00019 <span class="preprocessor">#include &quot;NetwToMacControlInfo.h&quot;</span>
<a name="l00020"></a>00020 <span class="preprocessor">#include &quot;BaseMacLayer.h&quot;</span>
<a name="l00021"></a>00021 <span class="preprocessor">#include &quot;FindModule.h&quot;</span>
<a name="l00022"></a>00022 
<a name="l00044"></a><a class="code" href="a00197.html">00044</a> <span class="keyword">class </span><a class="code" href="a00197.html" title="This is an implementation of a simple network layer which provides only some simple routing (next hop...">SimpleNetwLayer</a> : <span class="keyword">public</span> <a class="code" href="a00027.html" title="Base class for all simple modules of a host.">BaseModule</a>{
<a name="l00045"></a>00045 <span class="keyword">public</span>:
<a name="l00046"></a>00046   <span class="keyword">typedef</span> NetwPkt    netwpkt_t;
<a name="l00047"></a>00047   <span class="keyword">typedef</span> netwpkt_t* netwpkt_ptr_t;
<a name="l00048"></a>00048 <span class="comment">//--------members----------</span>
<a name="l00049"></a>00049 <span class="keyword">private</span>:
<a name="l00052"></a>00052   <a class="code" href="a00197.html" title="This is an implementation of a simple network layer which provides only some simple routing (next hop...">SimpleNetwLayer</a>(<span class="keyword">const</span> <a class="code" href="a00197.html" title="This is an implementation of a simple network layer which provides only some simple routing (next hop...">SimpleNetwLayer</a>&amp;);
<a name="l00055"></a>00055   <a class="code" href="a00197.html" title="This is an implementation of a simple network layer which provides only some simple routing (next hop...">SimpleNetwLayer</a>&amp; <a class="code" href="a00197.html#a76441caa603cc1d8852254c7e46c169b" title="Assignment operator is not allowed.">operator=</a>(<span class="keyword">const</span> <a class="code" href="a00197.html" title="This is an implementation of a simple network layer which provides only some simple routing (next hop...">SimpleNetwLayer</a>&amp;);
<a name="l00056"></a>00056 
<a name="l00057"></a>00057 <span class="keyword">protected</span>:
<a name="l00058"></a>00058   <span class="keywordtype">bool</span> isSwitch;
<a name="l00060"></a><a class="code" href="a00197.html#ae44adf1777151868d82ca8aba469378b">00060</a>   simtime_t <a class="code" href="a00197.html#ae44adf1777151868d82ca8aba469378b" title="time after when the module gets bored and starts jabbering.">boredTime</a>;
<a name="l00061"></a>00061 
<a name="l00062"></a>00062   <span class="keywordtype">int</span> maxTtl;
<a name="l00063"></a>00063 
<a name="l00064"></a>00064   <a class="code" href="a00117.html#ad85cd9e8d9e7e9cd5d02dd740c4a12a4" title="Type definition for a L3 (Network) address.">LAddress::L3Type</a> ip;
<a name="l00065"></a>00065 
<a name="l00066"></a>00066   <span class="keywordtype">int</span> dataIn;
<a name="l00067"></a>00067   <span class="keywordtype">int</span> dataOut;
<a name="l00068"></a>00068 
<a name="l00069"></a>00069   cMessage* startJabberTimer;
<a name="l00070"></a>00070 
<a name="l00071"></a>00071   <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> runningSeqNumber;
<a name="l00072"></a>00072 
<a name="l00073"></a>00073   <span class="keyword">typedef</span> std::map&lt;LAddress::L3Type, LAddress::L2Type&gt; RoutingTable;
<a name="l00074"></a>00074 
<a name="l00075"></a>00075   RoutingTable routingTable;
<a name="l00076"></a>00076 
<a name="l00077"></a>00077   <span class="keyword">enum</span> NetwPktKind{
<a name="l00078"></a>00078     HELLO_WORLD = 4200,
<a name="l00079"></a>00079     JABBER,
<a name="l00080"></a>00080     START_TO_JABBER
<a name="l00081"></a>00081   };
<a name="l00082"></a>00082 
<a name="l00083"></a>00083 <span class="comment">//--------methods----------</span>
<a name="l00084"></a>00084 <span class="keyword">protected</span>:
<a name="l00085"></a>00085   <span class="keywordtype">void</span> scheduleJabbering(){
<a name="l00086"></a>00086     <span class="keywordflow">if</span>(startJabberTimer-&gt;isScheduled()){
<a name="l00087"></a>00087       cancelEvent(startJabberTimer);
<a name="l00088"></a>00088     }
<a name="l00089"></a>00089 
<a name="l00090"></a>00090     scheduleAt(simTime() + <a class="code" href="a00197.html#ae44adf1777151868d82ca8aba469378b" title="time after when the module gets bored and starts jabbering.">boredTime</a>, startJabberTimer);
<a name="l00091"></a>00091   }
<a name="l00092"></a>00092 
<a name="l00093"></a>00093   <span class="keywordtype">void</span> broadcastHelloWorld() {
<a name="l00094"></a>00094     assert(!ev.isDisabled());
<a name="l00095"></a>00095     ev &lt;&lt; <span class="stringliteral">&quot;Broadcasting hello world.\n&quot;</span>;
<a name="l00096"></a>00096     netwpkt_ptr_t helloWorld = <span class="keyword">new</span> netwpkt_t(<span class="stringliteral">&quot;helloWorld&quot;</span>, HELLO_WORLD);
<a name="l00097"></a>00097 
<a name="l00098"></a>00098     helloWorld-&gt;setDestAddr(<a class="code" href="a00117.html#a11d63e8d897e072ed94da77028d38f60" title="Broadcast address for L3 addresses.">LAddress::L3BROADCAST</a>);
<a name="l00099"></a>00099     helloWorld-&gt;setSrcAddr(ip);
<a name="l00100"></a>00100     helloWorld-&gt;setSeqNum(runningSeqNumber++);
<a name="l00101"></a>00101     helloWorld-&gt;setTtl(maxTtl);
<a name="l00102"></a>00102 
<a name="l00103"></a>00103     <a class="code" href="a00157.html#a9556c1d066423e39247217d7a7f2a456" title="Attaches a &quot;control info&quot; structure (object) to the message pMsg.">NetwToMacControlInfo::setControlInfo</a>(helloWorld, <a class="code" href="a00117.html#a9f975ad0433f1fb8a01164f266a41f62" title="Broadcast address for L2 addresses.">LAddress::L2BROADCAST</a>);
<a name="l00104"></a>00104 
<a name="l00105"></a>00105     <span class="keyword">const_cast&lt;</span>cModule*<span class="keyword">&gt;</span>(<a class="code" href="a00027.html#af7e5a2e34b218afcc9b92d75e5e11df8" title="Get a reference to the local node module.">getNode</a>())-&gt;bubble(<span class="stringliteral">&quot;Hello World!&quot;</span>);
<a name="l00106"></a>00106 
<a name="l00107"></a>00107     sendDown(helloWorld);
<a name="l00108"></a>00108   }
<a name="l00109"></a>00109 
<a name="l00110"></a>00110   <span class="keywordtype">void</span> sendDown(cPacket* pkt) {
<a name="l00111"></a>00111     send(pkt, dataOut);
<a name="l00112"></a>00112   }
<a name="l00113"></a>00113 
<a name="l00114"></a>00114   <span class="keywordtype">void</span> forwardPacket(netwpkt_ptr_t pkt, <span class="keyword">const</span> <a class="code" href="a00117.html#aea56b60dcb5ae8c2bde465271daf7210" title="Type definition for a L2 (MAC) address.">LAddress::L2Type</a>&amp; nextHop){
<a name="l00115"></a>00115     netwpkt_ptr_t fwd = <span class="keyword">new</span> netwpkt_t(pkt-&gt;getName(), pkt-&gt;getKind());
<a name="l00116"></a>00116 
<a name="l00117"></a>00117     fwd-&gt;setDestAddr(pkt-&gt;getDestAddr());
<a name="l00118"></a>00118     fwd-&gt;setSrcAddr(pkt-&gt;getSrcAddr());
<a name="l00119"></a>00119     fwd-&gt;setSeqNum(pkt-&gt;getSeqNum());
<a name="l00120"></a>00120     fwd-&gt;setTtl(pkt-&gt;getTtl() - 1);
<a name="l00121"></a>00121 
<a name="l00122"></a>00122     <a class="code" href="a00157.html#a9556c1d066423e39247217d7a7f2a456" title="Attaches a &quot;control info&quot; structure (object) to the message pMsg.">NetwToMacControlInfo::setControlInfo</a>(fwd, nextHop);
<a name="l00123"></a>00123 
<a name="l00124"></a>00124     sendDown(fwd);
<a name="l00125"></a>00125   }
<a name="l00126"></a>00126 
<a name="l00127"></a>00127   <span class="keywordtype">void</span> handleHelloWorld(netwpkt_ptr_t pkt){
<a name="l00128"></a>00128 
<a name="l00129"></a>00129     <span class="comment">//who said hello?</span>
<a name="l00130"></a>00130     <span class="keyword">const</span> <a class="code" href="a00117.html#ad85cd9e8d9e7e9cd5d02dd740c4a12a4" title="Type definition for a L3 (Network) address.">LAddress::L3Type</a>&amp; srcIP = pkt-&gt;getSrcAddr();
<a name="l00131"></a>00131 
<a name="l00132"></a>00132     <span class="comment">//we already know ourself...</span>
<a name="l00133"></a>00133     <span class="keywordflow">if</span> (srcIP == ip){
<a name="l00134"></a>00134       <span class="keyword">delete</span> pkt;
<a name="l00135"></a>00135       <span class="keywordflow">return</span>;
<a name="l00136"></a>00136     }
<a name="l00137"></a>00137 
<a name="l00138"></a>00138     <span class="comment">//do we already know him?</span>
<a name="l00139"></a>00139     <span class="keywordflow">if</span>(routingTable.count(srcIP) == 0){
<a name="l00140"></a>00140       <span class="comment">//if not add him with the mac address of the previous hop</span>
<a name="l00141"></a>00141       <a class="code" href="a00132.html" title="Stores control information from mac to upper layer.">MacToNetwControlInfo</a>* cInfo = <span class="keyword">static_cast&lt;</span><a class="code" href="a00132.html" title="Stores control information from mac to upper layer.">MacToNetwControlInfo</a>*<span class="keyword">&gt;</span>(pkt-&gt;getControlInfo());
<a name="l00142"></a>00142 
<a name="l00143"></a>00143       <span class="keyword">const</span> <a class="code" href="a00117.html#aea56b60dcb5ae8c2bde465271daf7210" title="Type definition for a L2 (MAC) address.">LAddress::L2Type</a>&amp; prevHop = cInfo-&gt;<a class="code" href="a00132.html#a308453ed9a395b5150618bca3294d0b9" title="Returns the MAC address of the packets last hop.">getLastHopMac</a>();
<a name="l00144"></a>00144 
<a name="l00145"></a>00145       routingTable[srcIP] = prevHop;
<a name="l00146"></a>00146 
<a name="l00147"></a>00147       std::stringstream osBuff(std::stringstream::out);
<a name="l00148"></a>00148       osBuff &lt;&lt; <span class="stringliteral">&quot;Got hello from &quot;</span> &lt;&lt; srcIP;
<a name="l00149"></a>00149       <span class="keyword">const_cast&lt;</span>cModule*<span class="keyword">&gt;</span>(<a class="code" href="a00027.html#af7e5a2e34b218afcc9b92d75e5e11df8" title="Get a reference to the local node module.">getNode</a>())-&gt;bubble(osBuff.str().c_str());
<a name="l00150"></a>00150       ev &lt;&lt; osBuff.str() &lt;&lt; std::endl;
<a name="l00151"></a>00151     }
<a name="l00152"></a>00152 
<a name="l00153"></a>00153     <span class="comment">//if we are a switch and the time to live of the packet</span>
<a name="l00154"></a>00154     <span class="comment">//hasn&#39;t exceeded yet forward it</span>
<a name="l00155"></a>00155     <span class="keywordflow">if</span>(isSwitch){
<a name="l00156"></a>00156       <span class="keywordflow">if</span>(pkt-&gt;getTtl() &gt; 0) {
<a name="l00157"></a>00157         forwardPacket(pkt, <a class="code" href="a00117.html#a9f975ad0433f1fb8a01164f266a41f62" title="Broadcast address for L2 addresses.">LAddress::L2BROADCAST</a>);
<a name="l00158"></a>00158 
<a name="l00159"></a>00159         std::stringstream osBuff(std::stringstream::out);
<a name="l00160"></a>00160         osBuff &lt;&lt; srcIP &lt;&lt; <span class="stringliteral">&quot; said hello!&quot;</span>;
<a name="l00161"></a>00161         <span class="keyword">const_cast&lt;</span>cModule*<span class="keyword">&gt;</span>(<a class="code" href="a00027.html#af7e5a2e34b218afcc9b92d75e5e11df8" title="Get a reference to the local node module.">getNode</a>())-&gt;bubble(osBuff.str().c_str());
<a name="l00162"></a>00162         ev &lt;&lt; osBuff.str() &lt;&lt; std::endl;
<a name="l00163"></a>00163       }
<a name="l00164"></a>00164     } <span class="keywordflow">else</span> {
<a name="l00165"></a>00165       <span class="comment">//otherwise reset the bored timer after when we will start jabbering</span>
<a name="l00166"></a>00166       scheduleJabbering();
<a name="l00167"></a>00167     }
<a name="l00168"></a>00168 
<a name="l00169"></a>00169     <span class="keyword">delete</span> pkt;
<a name="l00170"></a>00170   }
<a name="l00171"></a>00171 
<a name="l00172"></a>00172   <span class="keywordtype">void</span> jabberToSomeone(){
<a name="l00173"></a>00173     assert(!isSwitch);
<a name="l00174"></a>00174     assert(routingTable.size() &gt; 0);
<a name="l00175"></a>00175     <span class="keywordtype">int</span> target = intrand(routingTable.size());
<a name="l00176"></a>00176 
<a name="l00177"></a>00177 
<a name="l00178"></a>00178     RoutingTable::const_iterator it = routingTable.begin();
<a name="l00179"></a>00179     <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i = 0; i &lt; target; ++i)
<a name="l00180"></a>00180       ++it;
<a name="l00181"></a>00181 
<a name="l00182"></a>00182     ev &lt;&lt; <span class="stringliteral">&quot;Jabbering - Routingtablesize:&quot;</span> &lt;&lt; routingTable.size() &lt;&lt; <span class="stringliteral">&quot;  target:&quot;</span> &lt;&lt; target &lt;&lt; <span class="stringliteral">&quot;  dest:&quot;</span> &lt;&lt; it-&gt;first &lt;&lt; endl;
<a name="l00183"></a>00183 
<a name="l00184"></a>00184     netwpkt_ptr_t jabber = <span class="keyword">new</span> netwpkt_t(<span class="stringliteral">&quot;jabber&quot;</span>, JABBER);
<a name="l00185"></a>00185 
<a name="l00186"></a>00186     jabber-&gt;setDestAddr(it-&gt;first);
<a name="l00187"></a>00187     jabber-&gt;setSrcAddr(ip);
<a name="l00188"></a>00188     jabber-&gt;setSeqNum(runningSeqNumber++);
<a name="l00189"></a>00189     jabber-&gt;setTtl(maxTtl);
<a name="l00190"></a>00190 
<a name="l00191"></a>00191     <a class="code" href="a00157.html#a9556c1d066423e39247217d7a7f2a456" title="Attaches a &quot;control info&quot; structure (object) to the message pMsg.">NetwToMacControlInfo::setControlInfo</a>(jabber, it-&gt;second);
<a name="l00192"></a>00192 
<a name="l00193"></a>00193     std::stringstream osBuff(std::stringstream::out);
<a name="l00194"></a>00194     osBuff &lt;&lt; <span class="stringliteral">&quot;Babbling with &quot;</span> &lt;&lt; it-&gt;first;
<a name="l00195"></a>00195     <span class="keyword">const_cast&lt;</span>cModule*<span class="keyword">&gt;</span>(<a class="code" href="a00027.html#af7e5a2e34b218afcc9b92d75e5e11df8" title="Get a reference to the local node module.">getNode</a>())-&gt;bubble(osBuff.str().c_str());
<a name="l00196"></a>00196     ev &lt;&lt; osBuff.str() &lt;&lt; std::endl;
<a name="l00197"></a>00197     sendDown(jabber);
<a name="l00198"></a>00198 
<a name="l00199"></a>00199     scheduleJabbering();
<a name="l00200"></a>00200   }
<a name="l00201"></a>00201 
<a name="l00202"></a>00202   <span class="keywordtype">void</span> handleIncomingJabber(netwpkt_ptr_t pkt){
<a name="l00203"></a>00203     <span class="keywordflow">if</span>(isSwitch) {
<a name="l00204"></a>00204       <span class="keywordflow">if</span>(pkt-&gt;getDestAddr() != ip){
<a name="l00205"></a>00205         assert(pkt-&gt;getTtl() &gt; 0);
<a name="l00206"></a>00206         assert(routingTable.count(pkt-&gt;getDestAddr()) &gt; 0);
<a name="l00207"></a>00207 
<a name="l00208"></a>00208         <a class="code" href="a00117.html#aea56b60dcb5ae8c2bde465271daf7210" title="Type definition for a L2 (MAC) address.">LAddress::L2Type</a> nextHop = routingTable[pkt-&gt;getDestAddr()];
<a name="l00209"></a>00209 
<a name="l00210"></a>00210         std::stringstream osBuff(std::stringstream::out);
<a name="l00211"></a>00211         osBuff &lt;&lt; pkt-&gt;getSrcAddr() &lt;&lt; <span class="stringliteral">&quot; babbles with &quot;</span> &lt;&lt; pkt-&gt;getDestAddr();
<a name="l00212"></a>00212         <span class="keyword">const_cast&lt;</span>cModule*<span class="keyword">&gt;</span>(<a class="code" href="a00027.html#af7e5a2e34b218afcc9b92d75e5e11df8" title="Get a reference to the local node module.">getNode</a>())-&gt;bubble(osBuff.str().c_str());
<a name="l00213"></a>00213         ev &lt;&lt; osBuff.str() &lt;&lt; std::endl;
<a name="l00214"></a>00214 
<a name="l00215"></a>00215         forwardPacket(pkt, nextHop);
<a name="l00216"></a>00216       } <span class="keywordflow">else</span> {
<a name="l00217"></a>00217         std::stringstream osBuff(std::stringstream::out);
<a name="l00218"></a>00218         osBuff &lt;&lt; pkt-&gt;getSrcAddr() &lt;&lt; <span class="stringliteral">&quot; babbles with me. But I&#39;m a serious switch, I do not babble...&quot;</span>;
<a name="l00219"></a>00219         <span class="keyword">const_cast&lt;</span>cModule*<span class="keyword">&gt;</span>(<a class="code" href="a00027.html#af7e5a2e34b218afcc9b92d75e5e11df8" title="Get a reference to the local node module.">getNode</a>())-&gt;bubble(osBuff.str().c_str());
<a name="l00220"></a>00220         ev &lt;&lt; osBuff.str() &lt;&lt; std::endl;
<a name="l00221"></a>00221       }
<a name="l00222"></a>00222     } <span class="keywordflow">else</span> {
<a name="l00223"></a>00223       assert(pkt-&gt;getDestAddr() == ip);
<a name="l00224"></a>00224 
<a name="l00225"></a>00225       std::stringstream osBuff(std::stringstream::out);
<a name="l00226"></a>00226       osBuff &lt;&lt; <span class="stringliteral">&quot;Got babbling from &quot;</span> &lt;&lt; pkt-&gt;getSrcAddr();
<a name="l00227"></a>00227       <span class="keyword">const_cast&lt;</span>cModule*<span class="keyword">&gt;</span>(<a class="code" href="a00027.html#af7e5a2e34b218afcc9b92d75e5e11df8" title="Get a reference to the local node module.">getNode</a>())-&gt;bubble(osBuff.str().c_str());
<a name="l00228"></a>00228       ev &lt;&lt; osBuff.str() &lt;&lt; std::endl;
<a name="l00229"></a>00229     }
<a name="l00230"></a>00230 
<a name="l00231"></a>00231     <span class="keyword">delete</span> pkt;
<a name="l00232"></a>00232   }
<a name="l00233"></a>00233 
<a name="l00234"></a>00234 <span class="keyword">public</span>:
<a name="l00235"></a>00235   <a class="code" href="a00197.html" title="This is an implementation of a simple network layer which provides only some simple routing (next hop...">SimpleNetwLayer</a>()
<a name="l00236"></a>00236     : <a class="code" href="a00027.html" title="Base class for all simple modules of a host.">BaseModule</a>()
<a name="l00237"></a>00237     , isSwitch(false)
<a name="l00238"></a>00238     , <a class="code" href="a00197.html#ae44adf1777151868d82ca8aba469378b" title="time after when the module gets bored and starts jabbering.">boredTime</a>()
<a name="l00239"></a>00239     , maxTtl(0)
<a name="l00240"></a>00240     , ip()
<a name="l00241"></a>00241     , dataIn(-1)
<a name="l00242"></a>00242     , dataOut(-1)
<a name="l00243"></a>00243     , startJabberTimer(NULL)
<a name="l00244"></a>00244     , runningSeqNumber(0)
<a name="l00245"></a>00245     , routingTable()
<a name="l00246"></a>00246   {}
<a name="l00247"></a>00247   <span class="keyword">virtual</span> ~<a class="code" href="a00197.html" title="This is an implementation of a simple network layer which provides only some simple routing (next hop...">SimpleNetwLayer</a>() {
<a name="l00248"></a>00248     cancelAndDelete(startJabberTimer);
<a name="l00249"></a>00249   }
<a name="l00250"></a>00250 
<a name="l00251"></a><a class="code" href="a00197.html#a20a43187e836ef7fb9c26a6132b3b77a">00251</a>   <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="a00197.html#a20a43187e836ef7fb9c26a6132b3b77a" title="Basic initialization for all modules.">initialize</a>(<span class="keywordtype">int</span> stage){
<a name="l00252"></a>00252 
<a name="l00253"></a>00253     <span class="keywordflow">if</span>(stage == 0){
<a name="l00254"></a>00254       dataOut = findGate(<span class="stringliteral">&quot;lowerLayerOut&quot;</span>);
<a name="l00255"></a>00255       dataIn = findGate(<span class="stringliteral">&quot;lowerLayerIn&quot;</span>);
<a name="l00256"></a>00256 
<a name="l00257"></a>00257       isSwitch = par(<span class="stringliteral">&quot;isSwitch&quot;</span>).boolValue();
<a name="l00258"></a>00258 
<a name="l00259"></a>00259       <span class="keywordflow">if</span>(isSwitch) {
<a name="l00260"></a>00260         <a class="code" href="a00027.html#aabf0226159b22b46e70031fd3bb3b1b2" title="Function to get a pointer to the host module.">FindModule&lt;&gt;::findHost</a>(<span class="keyword">this</span>)-&gt;getDisplayString().setTagArg(<span class="stringliteral">&quot;i&quot;</span>,0,<span class="stringliteral">&quot;device/accesspoint&quot;</span>);
<a name="l00261"></a>00261       }
<a name="l00262"></a>00262       ip = <a class="code" href="a00117.html#ad85cd9e8d9e7e9cd5d02dd740c4a12a4" title="Type definition for a L3 (Network) address.">LAddress::L3Type</a>(par(<span class="stringliteral">&quot;ip&quot;</span>).longValue());
<a name="l00263"></a>00263       maxTtl = par(<span class="stringliteral">&quot;maxTtl&quot;</span>).longValue();
<a name="l00264"></a>00264       <a class="code" href="a00197.html#ae44adf1777151868d82ca8aba469378b" title="time after when the module gets bored and starts jabbering.">boredTime</a> = par(<span class="stringliteral">&quot;boredTime&quot;</span>).doubleValue();
<a name="l00265"></a>00265     } <span class="keywordflow">else</span> <span class="keywordflow">if</span>(stage == 1) {
<a name="l00266"></a>00266       startJabberTimer = <span class="keyword">new</span> cMessage(<span class="stringliteral">&quot;jabber!&quot;</span>, START_TO_JABBER);
<a name="l00267"></a>00267       broadcastHelloWorld();
<a name="l00268"></a>00268     }
<a name="l00269"></a>00269   }
<a name="l00270"></a>00270 
<a name="l00271"></a>00271   <span class="keyword">virtual</span> <span class="keywordtype">void</span> handleMessage(cMessage* msg){
<a name="l00272"></a>00272 
<a name="l00273"></a>00273     <span class="keywordflow">switch</span>(msg-&gt;getKind()){
<a name="l00274"></a>00274     <span class="keywordflow">case</span> HELLO_WORLD:
<a name="l00275"></a>00275       handleHelloWorld(static_cast&lt;netwpkt_ptr_t&gt;(msg));
<a name="l00276"></a>00276       <span class="keywordflow">break</span>;
<a name="l00277"></a>00277     <span class="keywordflow">case</span> START_TO_JABBER:
<a name="l00278"></a>00278       jabberToSomeone();
<a name="l00279"></a>00279       <span class="keywordflow">break</span>;
<a name="l00280"></a>00280     <span class="keywordflow">case</span> JABBER:
<a name="l00281"></a>00281       handleIncomingJabber(static_cast&lt;netwpkt_ptr_t&gt;(msg));
<a name="l00282"></a>00282       <span class="keywordflow">break</span>;
<a name="l00283"></a>00283 
<a name="l00284"></a>00284     <span class="keywordflow">case</span> <a class="code" href="a00026.html#a49e0e548a6c20d002bd67afa7b76c483adebf9c77bbe6f19772866cccbef0fe4b">BaseMacLayer::PACKET_DROPPED</a>:
<a name="l00285"></a>00285       ev &lt;&lt; <span class="stringliteral">&quot;Packet dropped by MAC layer.&quot;</span> &lt;&lt; endl;
<a name="l00286"></a>00286       <span class="keyword">delete</span> msg;
<a name="l00287"></a>00287       <span class="keywordflow">break</span>;
<a name="l00288"></a>00288 
<a name="l00289"></a>00289     <span class="keywordflow">default</span>:
<a name="l00290"></a>00290       error(<span class="stringliteral">&quot;unknown packet type of packet %s&quot;</span>, msg-&gt;getName());
<a name="l00291"></a>00291       <span class="keywordflow">break</span>;
<a name="l00292"></a>00292     }
<a name="l00293"></a>00293   }
<a name="l00294"></a>00294 
<a name="l00295"></a>00295 
<a name="l00296"></a>00296 };
<a name="l00297"></a>00297 
<a name="l00298"></a>00298 <span class="preprocessor">#endif </span><span class="comment">/* SIMPLENETWLAYER_H_ */</span>
</pre></div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><b>SimpleNetwLayer.h</b>      </li>

    <li class="footer">Generated on Tue Mar 5 2013 21:26:29 for MiXiM by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.5.1 </li>
   </ul>
 </div>


</body>
</html>
